package com.food.admin.controller;


import com.food.admin.dto.StoreInfoDetailDto;
import com.food.admin.dto.StoreInfoListDto;
import com.food.admin.entity.StoreInfo;
import com.food.admin.exception.MyParameterException;
import com.food.admin.form.StoreInfoIdForm;
import com.food.admin.form.StoreInfoListForm;
import com.food.admin.form.StoreInfoStateUpdateForm;
import com.food.admin.model.Result;
import com.food.admin.service.StoreInfoService;
import com.food.admin.util.MyPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 商家信息 前端控制器
 * </p>
 *
 * @author 帅得一
 * @since 2023年06月13日
 */
@RestController
@RequestMapping("/storeInfo")
@Api(tags = "商铺信息接口")
public class StoreInfoController {
    @Autowired
    private StoreInfoService storeInfoService;

    @GetMapping("/list")
    @ApiOperation("分页条件查询商铺信息列表")
    public Result listByPageAndCondition(@Valid StoreInfoListForm form, BindingResult br) {

        if (br.hasErrors()) {
            throw new MyParameterException(500, "参数校验失败");
        }

        MyPage<StoreInfoListDto> dtoMyPage = storeInfoService.listByPageAndCondition(form);
        return Result.success("200", "分页条件查询商铺列表成功", dtoMyPage);
    }

    @GetMapping("/detail")
    @ApiOperation("查询店铺详情")
    public Result viewDetail(@Valid StoreInfoIdForm form, BindingResult br) {

        if (br.hasErrors()) {
            throw new MyParameterException(500, "参数校验失败");
        }

        StoreInfoDetailDto detailDto = storeInfoService.getDetail(form);
        return Result.success("200", "查询商铺信息详情成功", detailDto);
    }

    @PostMapping("/updateState")
    @ApiOperation("冻结或激活店铺")
    public Result freezeStore(@RequestBody @Valid StoreInfoStateUpdateForm form, BindingResult br) {
        if (br.hasErrors()) {
            throw new MyParameterException(500, "参数校验失败");
        }
        System.out.println(form.getStoreInfoId());
        storeInfoService.updateState(form);
        return Result.success("200", "修改店铺状态成功", null);
    }

}